package org.boulderfoodrescue.webapp.client.managers;

import java.util.LinkedList;
import java.util.List;

import org.boulderfoodrescue.webapp.client.events.ChangeType;
import org.boulderfoodrescue.webapp.client.events.PickupUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.PickupUpdatedHandler;
import org.boulderfoodrescue.webapp.client.rpc.PickupServiceAsync;
import org.boulderfoodrescue.webapp.shared.Pickup;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PickupManager {
	private static PickupServiceAsync pickupRPC;
	private static EventBus eventBus;
	private static List<Pickup> pickups;

	public PickupManager(PickupServiceAsync pickupRPC, EventBus eventBus)
	{
		PickupManager.pickupRPC = pickupRPC;
		PickupManager.eventBus = eventBus;
		bind();
	}

	private void bind()
	{
		eventBus.addHandler(PickupUpdatedEvent.getType(),
				new PickupUpdatedHandler(){
			@Override
			public void onPickupAdded(PickupUpdatedEvent event) {
				if(pickups == null)
					pickups = new LinkedList<Pickup>();
				pickups.add(event.getValue());
			}
			@Override
			public void onPickupUpdated(PickupUpdatedEvent event) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onPickupRemoved(PickupUpdatedEvent event) {
				if(pickups != null)
					pickups.remove(event.getValue());
			}
		});
	}

	public static List<Pickup> getPickups(){
		if(pickups != null)
			return pickups;
		pickupRPC.getPickups(new AsyncCallback<List<Pickup>>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Could not load pickups");
			}

			@Override
			public void onSuccess(List<Pickup> result) {
				if(result == null)
					Window.alert("Could not load pickups from server");
				else{
					for(Pickup p : result){
						eventBus.fireEvent(new PickupUpdatedEvent(ChangeType.Added, p));
					}
				}
			}
		});
		return null;
	}

	public static void savePickup(final Pickup pickup) {
		pickupRPC.savePickup(pickup, new AsyncCallback<Long>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Could not save pickup");
			}

			@Override
			public void onSuccess(Long result) {
				if(result == null)
					Window.alert("Coulder not save pickup to server");
				else{
					if(pickup.getDatabaseKey() != null)
						eventBus.fireEvent(new PickupUpdatedEvent(ChangeType.Updated, pickup));
					else{
						eventBus.fireEvent(new PickupUpdatedEvent(ChangeType.Added, pickup));
						pickup.setDatabaseKey(result);
					}
				}
			}

		});
	}

	public static void removePickup(final Pickup pickup){
		pickupRPC.removePickup(pickup, new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Could not deltee pickup from server");
			}

			@Override
			public void onSuccess(Boolean result) {
				if(result == null || !result)
					Window.alert("Failed to delete pickup from server");
				else{
					eventBus.fireEvent(new PickupUpdatedEvent(ChangeType.Removed, pickup));
				}
			}

		});
	}
}
